08. Dockerfile Exercises

Building and Running a Docker Container

Dockerfile Exercises

In the next video, you will walk through the process of building and running containers from two separate Dockerfiles. After the video, you will have the chance to try this yourself!

FSND C4 L1 A08 Building And Running Container

Instructions for building and running a sample container

Exercise 1: Hello World

  • In a new directory for this exercise, create a file named ‘Dockerfile’.
  • In the Dockerfile, add the lines
  FROM  jessie-slim

  ENTRYPOINT ["echo", "hello world"]
  • Build the image from the same directory using the command
  docker build --tag test .

Note that the full stop . tells the docker build command using the Dockerfile found in the current directory.

  • Once the image is built, you can run the container with the command:
  docker run test -rm

Hello World Dockerfile Build

Task Description:

Build and run a container using the “Hello World” Dockerfile instructions above.

Task List:

Task Feedback:

Excellent work! Next you will build a Flask application.

Exercise 2: Flask Example

  • Download the Dockerfile and app.py from here and place them in a new directory for this exercise.
  • Build the image with the same command used in the previous exercise
  docker build -t test .

Here, the -t flag is an alternate way of writing --tag .

  • Run the container with the following command:
  docker run  -p 80:8080 test

In this command, you are binding port 8080 of the container to the port 80 of your local machine. The flask application

  • Curl the endpoint
  curl http://0.0.0.0/
  • When you are finished, get the id of the running container:
  docker ps
  • You can then use the id to stop the container:
  docker stop <Container Id>

Flask Application Dockerfile Build

Task Description:

Build and run a container using the Dockerfile and Flask application linked above.

Task List:

Task Feedback:

Excellent work!

Summary

In this concept, you practiced building two Docker containers from Dockerfiles. You also learned or revisited some of the commands and flags that are useful for this process, including:

  • docker build which will build an image based on a Dockerfile.
  • The --tag flag, which is used to name and optionally tag the image in the name:tag format.
  • The docker run command, which is used to run a container based on the image.
  • The -p flag can be used to map container ports to host machine ports.

Further Research

For more information about Dockerfiles and commands, see: